﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    public class OrgNameCompensator : ICompensater
    {
        #region Implementation of ICompensater

        private List<string> _CompositeFieldNames;
        public List<string> CompositeFieldNames
        {
            get { return _CompositeFieldNames; }
            set { this._CompositeFieldNames = value; }
        }

        private bool _CanSwap;
        public bool CanSwap
        {
            get { return _CanSwap; }
            set { _CanSwap = value; }
        }

        private double _CompensateScore;
        public double CompensateScore
        {
            get { return this._CompensateScore; }
            set { this._CompensateScore = value; }
        }

        public double Compensate(
            Dictionary<string, object> values1,
            Dictionary<string, object> values2)
        {
            if (values1 == null || values2 == null || values1.Count == 0 || values2.Count == 0)
                return 0.0;

            int countNotMatched = 0;
            OrganizationNameComparer nameComparer = new OrganizationNameComparer();
            foreach (string fieldName in this.CompositeFieldNames)
            {
                if (values1.ContainsKey(fieldName) && values2.ContainsKey(fieldName))
                {
                    object value1 = values1[fieldName];
                    object value2 = values2[fieldName];
                    double fieldScore = nameComparer.Compare(value1, value2, 0, null, string.Empty);
                    if (fieldScore <= 0)
                        countNotMatched++;
                }
            }
            if (countNotMatched == this.CompositeFieldNames.Count)
                return this._CompensateScore;
            else
                return 0.0;
        }

        public double Compensate(Dictionary<string, double> fieldScores)
        {
            bool foundFieldMatched = false;
            foreach (string fieldName in this.CompositeFieldNames)
            {
                if (fieldScores != null && fieldScores.ContainsKey(fieldName))
                {
                    if (fieldScores[fieldName] > 0)
                    {
                        foundFieldMatched = true;
                        break;
                    }
                }
            }
            if (!foundFieldMatched)
                return this._CompensateScore;
            else
                return 0.0;
        }


        #endregion
    }
}
